package com.woyowodaows.fragment.mine;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.woyowodaows.R;
import com.woyowodaows.activity.mine.OrderInformationActivity;
import com.woyowodaows.adapter.home.HomeOrderAdapter;
import com.woyowodaows.adapter.mine.ServiceManageMentAdapter;
import com.woyowodaows.base.BaseFragment;
import com.woyowodaows.bean.home.NearRepairBean;
import com.woyowodaows.bean.home.NearRepairData;
import com.woyowodaows.bean.home.OrderInfo;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.LoadingDataLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/29.
 *    报修管理   已接订单
 */

public class ServiceManagementHaveOrderFragement extends BaseFragment {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout swRefresh;
    @BindView(R.id.rl_load)
    LoadingDataLayout rlLoad;
    private ServiceManageMentAdapter mAapter;
    private List<NearRepairBean> repairList=new ArrayList<>();
    private HomeOrderAdapter mAdapter;
    private boolean isFirstLoad = false;//初始化为false
    private  String  status ="3";


    @Override
    protected void initView() {
        ButterKnife.bind(this, layoutView);
        mAdapter=new HomeOrderAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        rvData.setLayoutManager(layoutManager);
        rvData.setAdapter(mAdapter);
        getNearRepairData();
        swRefresh.setRefreshing(true);
    }

    @Override
    public void initEvent() {
        super.initEvent();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener<NearRepairBean>() {
            @Override
            public void onItemClick(BaseQuickAdapter<NearRepairBean, ? extends BaseViewHolder> adapter, View view, int position) {
                if(repairList.size()>0){
                    NearRepairBean bean= repairList.get(position);
                    Intent intent=new Intent(mContext,OrderInformationActivity.class);
                    intent.putExtra("OrderSn",bean.getOrderSn());
                    startActivity(intent);
                }
            }
        });
        rlLoad.setOnReloadListener(new LoadingDataLayout.OnReloadListener() {
            @Override
            public void onReload(View v, int status) {
                rlLoad.setStatus(LoadingDataLayout.LOADING);
                isRefresh = true;
                pageNumber = 0;
                maxPage = 1;
                getNearRepairData();
            }
        });
        swRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNumber = 0;
                maxPage = 1;
                getNearRepairData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (maxPage <= pageNumber) {
                    mAdapter.loadMoreEnd();
                } else {
                    isRefresh = false;
                    pageNumber++;
                    getNearRepairData();
                }
            }
        }, rvData);
    }
    @Override
    public void onResume() {
        super.onResume();
        isRefresh = true;
        pageNumber = 0;
        maxPage = 1;
        getNearRepairData();
    }
    //获取近期订单
    public void getNearRepairData() {
        final int tempPageNum = pageNumber;
        final int tempPageSize = pageSize;
        final Map<String, Object> mapParameter = new HashMap<>();
        //status= 1待接单 3.已接单 4.处理完成 不传或者传null 查全部
        mapParameter.put("pageNumber", pageNumber);
        mapParameter.put("pageSize", pageSize);
        mapParameter.put("status",status);//3
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.NEARREPAIR,NearRepairData.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    NearRepairData bean = (NearRepairData) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (isRefresh) {
                            repairList.clear();
                            maxPage = bean.getData().getTotalPage();
                        }
                        if(bean.getData().getList().size()>0){
                            repairList.addAll(bean.getData().getList());
                            mAdapter.setNewData(repairList);
                        }
                    } else {
                        pageSize = tempPageSize;
                        pageNumber = tempPageNum;
                        ToastUtil.showError(getActivity(), bean.getMessage());
                    }
                }
                if (repairList.size() > 0){
                    rlLoad.setStatus(LoadingDataLayout.SUCCESS);
                }else {
                    rlLoad.setStatus(LoadingDataLayout.EMPTY);
                }
                swRefresh.setRefreshing(false);
                mAdapter.loadMoreComplete();

            }

            @Override
            public void onError(String message) {
                pageSize = tempPageSize;
                pageNumber = tempPageNum;
                swRefresh.setRefreshing(false);
                mAdapter.loadMoreFail();
                if (repairList.size() > 0){
                    ToastUtil.show(mContext,message);
                }else {
                    rlLoad.setStatus(LoadingDataLayout.ERROR);
                }

            }
        });

    }
    @Override
    protected int getLayoutId() {
        isFirstLoad = true;//视图创建完成，将变量置为true
        return R.layout.fragment_service_management_have_order;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirstLoad && isVisibleToUser) {
            getNearRepairData();//数据加载操作
            isFirstLoad = false;
        }
    }
}
